/*
 * @lc app=leetcode.cn id=114 lang=golang
 *
 * [114] 二叉树展开为链表
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func flatten(root *TreeNode) {
	if root == nil {
		return
	}
	list := []*TreeNode{}

	var preOrder func(node *TreeNode)

	preOrder = func(node *TreeNode) {
		if node == nil {
			return
		}
		list = append(list, node)
		preOrder(node.Left)
		preOrder(node.Right)
	}
	preOrder(root)
	for i := 0; i < len(list)-1; i++ {
		list[i].Left = nil
		list[i].Right = list[i+1]

	}

}

// @lc code=end

